package com.stu.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.stu.config.db.ConnectionPool;
import com.stu.dao.StuDao;
import com.stu.model.Stu;

public class StuDaoImpl implements StuDao{
	private static StuDaoImpl dao=new StuDaoImpl();
	private StuDaoImpl() {}
	public static StuDaoImpl getStuDaoImpl() {
		return dao;
	}

	@Override
	public boolean saveOrUpdate(String sql) throws SQLException {
		Connection connection=ConnectionPool.getConnection();
		Statement stat=connection.createStatement();
		int b=stat.executeUpdate(sql);
		ConnectionPool.closeStat(stat);
		ConnectionPool.closeConn(connection);
		return b==1;
	}
	
	@Override
	public List<Stu> findAll(String sql) throws SQLException {
		Connection connection=ConnectionPool.getConnection();
		Statement stat=connection.createStatement();
		ResultSet rs=stat.executeQuery(sql);
		List<Stu> list=new ArrayList<Stu>();
		
		while(rs.next()) {
			Stu stu=new Stu();
			stu.setId(rs.getString("id"));
			stu.setLoginname(rs.getString("loginname"));
			stu.setPassword(rs.getString("password"));
			stu.setSex(rs.getString("sex"));
			list.add(stu);
			
		}
		ConnectionPool.closeRS(rs);
		ConnectionPool.closeStat(stat);
		ConnectionPool.closeConn(connection);
		return list;
	}

}
